package com.zlcdgroup.delegates

import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Assertions.assertThrowsExactly
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.text.SimpleDateFormat

class DelegatesValidatorTest {

    @Test
    fun notBlank() {
        assertThrows<AppException> {
            var name: String by DelegatesValidator.createBuilder("")
                .notBlank("")
                .build()
            name = ""
            println(name)
        }
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .notBlank("")
                .build()
            name = "notBlank"
            println(name)
        }
    }

    @Test
    fun sizeRange() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .sizeRange(4, 20, "")
                    .build()
                name = "123"
                println(name)
            }, "验证字符串长度"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .sizeRange(4, 20, "")
                .build()
            name = "sizeRange"
            println(name)
        }
    }

    @Test
    fun numberRang() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: Number by DelegatesValidator.createBuilder("")
                    .numberRang(4, 20, "")
                    .build()
                name = 3
                println(name)

            }, "验证数值范围"
        )
        assertDoesNotThrow {
            var name: Number by DelegatesValidator.createBuilder("")
                .numberRang(4, 20, "")
                .build()
            name = 6
            println(name)
        }
    }

    @Test
    fun email() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .email("")
                    .build()
                name = "peianskygmail.com"
                println(name)
            }, "验证邮件格式"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .email("")
                .build()
            name = "peiansky@gmail.com"
            println(name)
        }
    }

    @Test
    fun idCard() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .idCard("")
                    .build()
                name = "2222222222222222221"
                println(name)
            }, "验证身份证号"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .idCard("")
                .build()
            name = "222222222222222222"
            println(name)
        }
    }

    @Test
    fun mobilePhone() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .mobilePhone("")
                    .build()
                name = "136483222083"
                println(name)
            }, "验证电话号码"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .mobilePhone("")
                .build()
            name = "13648322208"
            println(name)
        }
    }

    @Test
    fun stringStart() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .stringStart(setOf("pj"), "")
                    .build()
                name = "p20240101"
                println(name)
            }, "验证字符串起始"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .stringStart(setOf("pj"), "")
                .build()
            name = "pj20240101"
            println(name)
        }
    }

    @Test
    fun stringEnd() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .stringEnd(setOf("pj"), "")
                    .build()
                name = "pj20240101"
                println(name)
            }, "验证字符串结尾"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .stringEnd(setOf("pj"), "")
                .build()
            name = "pj20240101pj"
            println(name)
        }
    }

    @Test
    fun stringContains() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .stringContains(setOf("pj"), "")
                    .build()
                name = "p20240101j"
                println(name)
            }, "验证字符串包含"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .stringContains(setOf("pj"), "")
                .build()
            name = "pj20240101pj"
            println(name)
        }
    }

    @Test
    fun numberInSet() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: Number by DelegatesValidator.createBuilder("")
                    .numberInSet(setOf(1, 2, 3), "")
                    .build()
                name = 0
                println(name)
            }, "验证数值范围"
        )
        assertDoesNotThrow {
            var name: Number by DelegatesValidator.createBuilder("")
                .numberInSet(setOf(1, 2, 3), "")
                .build()
            name = 2
            println(name)
        }
    }

    @Test
    fun stringInSet() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .stringInSet(setOf("2", "3", "4"), "")
                    .build()
                name = "23"
                println(name)
            }, "验证字符串值范围"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .stringInSet(setOf("2", "3", "4"), "")
                .build()
            name = "2"
            println(name)
        }
    }

    @Test
    fun date() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .date(SimpleDateFormat("yyyy-MM-dd"), "")
                    .build()
                name = "02-04"
                println(name)
            }, "验证日期格式"
        )
        assertDoesNotThrow {
            var name: String by DelegatesValidator.createBuilder("")
                .date(SimpleDateFormat("yyyy-MM-dd"), "")
                .build()
            name = "2024-02-04"
            println(name)
        }
    }

    @Test
    fun xss() {
        assertThrowsExactly(
            AppException::class.java,  {
                var name: String by DelegatesValidator.createBuilder("")
                    .xss( "")
                    .build()
                name = "\"<javascript>test</javascript>\""
                println(name)
            }, "验证xss"
        )
        assertDoesNotThrow {

            var name: String by DelegatesValidator.createBuilder("")
                .xss( "")
                .build()
            name = "\"<javasc3ript>test</javascript>\""
            println(name)
        }
    }

}